#include <windows.h>
#include <unistd.h>

#include <stdio.h>
#include <SDL/SDL.h>
#include "Triselection.h"

typedef struct
{
    SDL_Rect pos;
    int val;
} cases;


int main(int argc, char *argv[])
{
    SDL_Surface *screen;
    SDL_Surface *image, *image0,*image01, *image1, *image2, *image3, *image4, *image5, *imagep ;
    SDL_Rect src ,src0 ,src01 ,src1 , src2, src3, src4, src5,srcp, dest, dest0,dest01 , dest1, dest2, dest3,dest4 , dest5,destp;
    SDL_Event event ;
    cases T[5];
    int i,j,k, mini , p,done=0, trouve=0, aux1 , aux2 ;
    cases aux;

    T[0].val=9;
    T[1].val=4;
    T[2].val=20;
    T[3].val=12;
    T[4].val=2;

    T[0].pos.x=333 ;
    T[0].pos.y=227 ;

    T[1].pos.x=420 ;
    T[1].pos.y=223 ;

    T[2].pos.x=500 ;
    T[2].pos.y=220 ;

    T[3].pos.x=580 ;
    T[3].pos.y=220 ;

    T[4].pos.x=670 ;
    T[4].pos.y=230 ;
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 7;
    }
    screen = SDL_SetVideoMode(844,528, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 8;
    }

    image = SDL_LoadBMP("img/fond.bmp");
    if (image == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 9;
    }

    imagep = SDL_LoadBMP("img/play.bmp");
    if (imagep == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 33;
    }
SDL_SetColorKey(imagep, SDL_SRCCOLORKEY, SDL_MapRGB(imagep->format, 255, 255, 255));


    image0 = SDL_LoadBMP("img/tableau.bmp");
    if (image0 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 0;
    }

    image01=SDL_LoadBMP ("img/t0.bmp");
    if (image01==NULL)
    {
        printf("unable to load bitmap: %s\n",SDL_GetError());
        return (01);
    }

    image1 = SDL_LoadBMP("img/t1.bmp");
    if (image1 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
SDL_SetColorKey(image1, SDL_SRCCOLORKEY, SDL_MapRGB(image1->format, 255, 255, 255));
    image2 = SDL_LoadBMP("img/t2.bmp");
    if (image2 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 2;
    }
SDL_SetColorKey(image2, SDL_SRCCOLORKEY, SDL_MapRGB(image2->format, 255, 255, 255));
    image3 = SDL_LoadBMP("img/t3.bmp");
    if (image3 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 3;
    }
SDL_SetColorKey(image3, SDL_SRCCOLORKEY, SDL_MapRGB(image3->format, 255, 255, 255));
    image4 = SDL_LoadBMP("img/t4.bmp");
    if (image4 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 4;
    }
SDL_SetColorKey(image4, SDL_SRCCOLORKEY, SDL_MapRGB(image4->format, 255, 255, 255));
    image5 = SDL_LoadBMP("img/t5.bmp");
    if (image5 == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 5;
    }
SDL_SetColorKey(image5, SDL_SRCCOLORKEY, SDL_MapRGB(image5->format, 255, 255, 255));

    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;
    dest.x = 0;
    dest.y = 0;
    dest.w = image->w;
    dest.h = image->h;

    src0.x = 0;
    src0.y = 0;
    src0.w = image->w;
    src0.h = image->h;
    dest0.x = 300;
    dest0.y = 100;
    dest0.w = image->w;
    dest0.h = image->h;


    src01.x = 0;
    src01.y = 0;
    src01.w = image->w;
    src01.h = image->h;
    dest01.x = 300;
    dest01.y = 200;
    dest01.w = image->w;
    dest01.h = image->h;

   srcp.x = 0;
    srcp.y = 0;
    srcp.w = image->w;
    srcp.h = image->h;
    destp.x = 450;
    destp.y = 400;
    destp.w = image->w;
    destp.h = image->h;


    src1.x = 0;
    src1.y = 0;
    src1.w = image->w;
    src1.h = image->h;
    dest1.x = T[0].pos.x;
    dest1.y = T[0].pos.y;
    dest1.w = image->w;
    dest1.h = image->h;

    src2.x = 0;
    src2.y = 0;
    src2.w = image->w;
    src2.h = image->h;
    dest2.x = T[1].pos.x;
    dest2.y = T[1].pos.y;
    dest2.w = image->w;
    dest2.h = image->h;

    src3.x = 0;
    src3.y = 0;
    src3.w = image->w;
    src3.h = image->h;
    dest3.x = T[2].pos.x;
    dest3.y = T[2].pos.y;
    dest3.w = image->w;
    dest3.h = image->h;

    src4.x = 0;
    src4.y = 0;
    src4.w = image->w;
    src4.h = image->h;
    dest4.x = T[3].pos.x;
    dest4.y = T[3].pos.y;
    dest4.w = image->w;
    dest4.h = image->h;

    src5.x = 0;
    src5.y = 0;
    src5.w = image->w;
    src5.h = image->h;
    dest5.x = T[4].pos.x;
    dest5.y = T[4].pos.y;
    dest5.w = image->w;
    dest5.h = image->h;





    i=0;
   while ( i<5 || done || trouve ) //game loop
    {
        /************************** Game Logic : Tri des tableaux + ecoute des events **********************/



/*******************affichage du tableau initial (2)**************************/

        SDL_BlitSurface(image, &src, screen, &dest);
        SDL_BlitSurface(image0, &src0, screen, &dest0);
        SDL_BlitSurface(image01, &src01, screen, &dest01);
        SDL_BlitSurface(imagep, &srcp, screen, &destp);


 /*******************affichage du tableau resultat (2)**************************/
        SDL_BlitSurface(image1, NULL , screen, &T[0].pos);
        SDL_BlitSurface(image2, NULL, screen, &T[1].pos);
        SDL_BlitSurface(image3, NULL, screen, &T[2].pos);
        SDL_BlitSurface(image4, NULL ,screen, &T[3].pos);
        SDL_BlitSurface(image5, NULL, screen, &T[4].pos);


        /*********************ecoute des evenements ******************************/



        while(SDL_WaitEvent(&event) && !done)
        {
           switch(event.type)
            {
            case SDL_KEYDOWN:
              //
            if(event.key.keysym.sym == SDLK_SPACE)
                    done =1;

               break;


            }

        }

        /******************** Tri du tableau ************************/
if (T[i].val > T[i+1].val)
{
    aux1=T[i].pos.x;
    T[i].pos.x =T[i+1].pos.x;
    T[i+1].pos.x = aux1;
}
    else trouve=1;


        /*********************** copie de screen dans carte graphique (3 + 4) ***********************/

        SDL_Flip(screen);
i++;

       }


    SDL_FreeSurface(image1);
    SDL_FreeSurface(image2);
    SDL_FreeSurface(image3);
    SDL_FreeSurface(image4);
    SDL_FreeSurface(image5);
    return(0);

    }
